﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AI3SaveEditor.AI3Files
{
    public class Js3cfiFile : AI3File
    {
        List<ClothesSetting> clothesSettings;
        public List<ClothesSetting> ClothesSettings
        {
            get { return clothesSettings; }
            set { clothesSettings = value; }
        }

        public Js3cfiFile(string fileName)
            : base(fileName)
        {
            Load();
        }

        public override void Load()
        {
            this.buffer = IllusionUtils.CMIDecrypt.DecryptFile(this.file.FullName);
            LoadClothesSettings();
        }



        void LoadClothesSettings()
        {
            clothesSettings = new List<ClothesSetting>();
            foreach (ClothesSettingType type in Enum.GetValues(typeof(ClothesSettingType)))
            {
                clothesSettings.Add(new ClothesSetting(type, this.buffer));
            }
        }

        protected override void DecryptFileContent(byte[] fileContent)
        {
            IllusionUtils.CMIDecrypt.DecryptString(fileContent, 4, 8 * 128);
            IllusionUtils.CMIDecrypt.DecryptBuffer(fileContent);
        }
    }

    public class ClothesSetting
    {
        public ClothesSetting() { }
        byte[] buffer;

        public ClothesSetting(ClothesSettingType type, byte[] buffer)
        {
            this.buffer = buffer;
            this.Type = type;
            int start = ((int)type * 128) + 4;
            IllusionUtils.CMIDecrypt.DecryptString(this.buffer, start, 128);
            this.File = Read(start, 64);
            this.Name = Read(start + 64, 64);
        }

        string Read(int start, int length)
        {
            string result = IllusionUtils.GetStringWithTrimNull(buffer, start, 64);
            int index = result.IndexOf('\0');
            return index > -1 ? result.Substring(0, index) : result;
        }

        public ClothesSettingType Type { set; get; }

        string file;
        public string File
        {
            get { return file; }
            set
            {
                this.file = value;
                IllusionUtils.SetString(value, buffer, ((int)this.Type * 128)+4, 64);
            }
        }

        string name;

        public string Name
        {
            get { return name; }
            set
            {
                this.name = value;
                IllusionUtils.SetString(value, buffer, ((int)this.Type * 128) + 68, 64);
            }
        }

        public override string ToString()
        {
            return string.Format("{0}: {1} (文件名:{2})", this.Type, this.Name, this.File);
        }
    }

    public enum ClothesSettingType
    {
        第一 = 0,
        其次,
        夜间,
        睡觉,
        沐浴,
        可爱,
        性感,
        色欲
    }
}
